<template>
  <div :class="isBg ? 'box_bg box' : 'box'">
    <!-- Allocate chart inside div container -->
    <!-- <div class="icons">
      <span class="el-icon-full-screen" @click="openDrawer"></span>
    </div> -->
    <!-- Create div to render the chart into-->
    <div style="height: 100%" class="chart" ref="chart"></div>
    <!-- <ChartMsg :row="row"></ChartMsg> -->
  </div>
</template>

<script>
import mqtt from "mqtt";

function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
      break;
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
      break;
    default:
      return 0;
      break;
  }
}
export default {
  name: "Chart",
  props: ["row"],
  components: {
    // ChartMsg,
  },
  data() {
    return {
      chart: null,
      data: [],
      isBg: false,
      data2: [56, 80, 10, 30, 10, 80, 20, 1, 50, 20, 90],
      data1: [60, 4, 90, 5, 90, 40, 5, 90, 20, 90],
      time: [
        "10:12:23",
        "11:12:23",
        "12:12:23",
        "13:12:23",
        "14:12:23",
        "15:12:23",
        "16:12:23",
        "17:12:23",
        "18:12:23",
      ],
    };
  },

  mounted() {
    this.$nextTick(async () => {
      this.lineChart = this.$echarts.init(this.$refs.chart);

      this.init();
      // let i1 = 1;
      // let i2 = 1;
      // setInterval(() => {
      //   var myDate = new Date();
      //   // this.lineChart.clear();
      //   i1 += 20;
      //   i2 += 25;
      //   this.data1.push(i1);
      //   this.data2.push(i2);
      //   this.time.push(
      //     `${myDate.getHours()}:0${myDate.getMinutes()}:${myDate.getSeconds()}`
      //   );
      //   this.init();
      // }, 1000);
    });
  },
  methods: {
    init() {
      function randomData() {
        now = new Date(+now + oneDay);
        value = value + Math.random() * 21 - 10;
        return {
          name: now.toString(),
          value: [
            [now.getFullYear(), now.getMonth() + 1, now.getDate()].join("/"),
            Math.round(value),
          ],
        };
      }
      let now = new Date(1997, 9, 3);
      let oneDay = 24 * 3600 * 1000;
      let value = Math.random() * 1000;
      for (var i = 0; i < 1000; i++) {
        this.data.push(randomData());
      }
      var option = {
        legend: {
          x: "right", //可设定图例在左、右、居中
          // y: "center",
          // padding: 10,
          // icon: "rect",
          // itemWidth: 20,
          // itemHeight: 10,
          // itemGap: 10,
          orient: "vertical",
          data: [
            {
              name: "5米热压罐",
              textStyle: {
                color: "#65f5f3",
              },
            },
            {
              name: "10米热压罐",
              textStyle: {
                color: "#277ace",
              },
            },
          ],
        },
        title: {
          text: "热处理原始记录表单",
          left: "center",
          textStyle: {
            color: "#5dcbff",
          },
        },
        backgroundColor: "#1F295F",
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(0, 255, 233,0)",
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255, 255, 255,1)",
                  },
                  {
                    offset: 1,
                    color: "rgba(0, 255, 233,0)",
                  },
                ],
                global: false,
              },
            },
          },
        },
        // dataZoom: [
        //   {
        //     type: "inside",
        //     start: 0,
        //     end: 100,
        //     textStyle: {
        //       color: "#5dcbff",
        //     },
        //   },
        //   {
        //     start: 0,
        //     end: 100,
        //     textStyle: {
        //       color: "#5dcbff",
        //     },
        //   },
        // ],
        grid: {
          top: "25%",
          left: "14%",
          right: "3%",
          bottom: "15%",
          // containLabel: true
        },

        xAxis: [
          {
            type: "category",
            axisLine: {
              show: true,
              color: "#5dcbff",
            },

            axisLabel: {
              color: "#5dcbff",
              width: 100,
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#315070"],
                width: 1,
                type: "solid",
              },
            },
            boundaryGap: false,
            data: this.time, //this.$moment(data.times).format("HH-mm") ,
          },
        ],

        yAxis: [
          {
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#315070"],
                width: 1,
                type: "solid",
              },
            },
            type: "value",
            name: "10米(个)",
            position: "left",
            offset: 0,
            scale: true,
            axisTick: {
              show: true,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#277ace",
              },
            },
            axisLabel: {
              offset: 12,
              formatter: "{value} ",
            },
          },
          {
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#315070"],
                width: 1,
                type: "solid",
              },
            },
            type: "value",
            name: "5米(个)",
            position: "left",
            offset: 46,
            axisTick: {
              show: true,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#65f5f3",
              },
            },
            axisLabel: {
              formatter: "{value} ",
            },
          },
        ],
        series: [
          {
            name: "10米热压罐",
            type: "line",
            // smooth: true,
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 10,
            yAxisIndex: 0,
            // lineStyle: {
            //   normal: {
            //     color: "#277ace",
            //   },
            // },
            // label: {
            //   show: true,
            //   position: "top",
            //   textStyle: {
            //     color: "#277ace",
            //   },
            // },
            itemStyle: {
              normal: {
                color: "#0092f6",
                lineStyle: {
                  color: "#0092f6",
                  width: 1,
                },
                areaStyle: {
                  //color: '#94C9EC'
                  color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                    {
                      offset: 0,
                      color: "rgba(7,44,90,0.3)",
                    },
                    {
                      offset: 1,
                      color: "rgba(0,146,246,0.9)",
                    },
                  ]),
                },
              },
            },
            markPoint: {
              itemStyle: {
                normal: {
                  color: "red",
                },
              },
            },
            data: this.data1, //data.values
          },
          {
            yAxisIndex: 1,
            // smooth: true,
            name: "5米热压罐",
            type: "line",
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 10,
            // lineStyle: {
            //   normal: {
            //     color: "#65f5f3",
            //   },
            // },
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "#65f5f3",
              },
            },
            itemStyle: {
              normal: {
                color: "#00d4c7",
                lineStyle: {
                  color: "#00d4c7",
                  width: 1,
                },
                areaStyle: {
                  //color: '#94C9EC'
                  color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                    {
                      offset: 0,
                      color: "rgba(7,44,90,0.3)",
                    },
                    {
                      offset: 1,
                      color: "rgba(0,212,199,0.9)",
                    },
                  ]),
                },
              },
            },
            data: this.data2, //data.values
          },
        ],
      };

      this.lineChart.setOption(option);
      // this.$nextTick(() => {
      //   this.lineChart = this.$echarts.init(this.$refs.chart);
      //   this.lineChart.setOption(option);
      //   setInterval(() => {
      //     for (var i = 0; i < 5; i++) {
      //       this.data.shift();
      //       this.data.push(randomData());
      //     }
      //     this.lineChart.setOption({
      //       series: [
      //         {
      //           data: this.data,
      //         },
      //       ],
      //     });
      //   }, 1000);
      // });
    },
    openDrawer() {
      // this.isBg = !this.isBg;
    },
  },
};
</script>

<style scoped lang='scss'>
.fill {
  /* height: 320px; */
  height: 100%;
  width: 100%;
}

.box {
  display: flex;
  flex-flow: column;
  width: 100%;
  position: relative;
  height: 53%;
  width: 100%;
  border: 1px solid #0da9f2;
  .icons {
    position: absolute;
    right: 20px;
    top: 16px;
    z-index: 2;
  }
}
.box_bg {
  position: absolute;
  left: 20%;
  z-index: 999;
  width: 80%;
  height: 76%;
}
.box .row.header {
  flex: 0 1 auto;
}

.box .row.content {
  flex: 1 1 auto;
}
</style>
